﻿using GCC.Container.Abstracts;

using CacheManager = GCC.Container.Cache.Manager;

namespace GCC.APIContainer
{
    /// <summary>
    /// 容器上下文
    /// </summary>
    public partial class GCCContext : AbsGCCContext
    {
        private readonly CacheManager _cache;

        /// <summary>
        /// 根据Key获取,如果获取不到则新增
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">key</param>
        /// <param name="onSet">当获取不到的时候从哪获取值</param>
        /// <param name="settings">过期时间</param>
        /// <param name="cancellationToken">取消token</param>
        /// <returns></returns>
        public override Task<T> GetOrSet<T>(string key, Func<Task<T>> onSet, CacheItemSettings settings, CancellationToken cancellationToken = default)
        {
            return _cache.GetOrSet<T>(key, onSet, settings, cancellationToken);
        }

        /// <summary>
        /// 刷新一下某个缓存
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="cancellationToken">取消token</param>
        public override Task Refresh(string key, CancellationToken cancellationToken = default)
        {
            return _cache.Refresh(key, cancellationToken);
        }


        /// <summary>
        /// 移除一个缓存
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="cancellationToken">取消token</param>
        public override Task Remove(string key, CancellationToken cancellationToken = default)
        {
            return _cache.Remove(key, cancellationToken);
        }

        /// <summary>
        /// 设置一个值到缓存
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">key</param>
        /// <param name="data">数据</param>
        /// <param name="settings">过期时间</param>
        /// <param name="cancellationToken">取消token</param>
        public override Task Set<T>(string key, T data, CacheItemSettings settings, CancellationToken cancellationToken = default)
        {
            return _cache.Set<T>(key, data, settings, cancellationToken);
        }

        /// <summary>
        /// 根据key获取某一个缓存,自己用优先级判定
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="key">key</param>
        /// <param name="cancellationToken">取消token</param>
        /// <returns>从缓存获取的值,可能为空</returns>
        public override Task<T> TryGet<T>(string key, CancellationToken cancellationToken = default)
        {
            return _cache.TryGet<T>(key, cancellationToken);
        }






    }
}
